# Alexander F. Gazmararian
# afg2@princeton.edu

# Load packages
library(tidyverse)
library(modelsummary)

# Load data
g <- readRDS("data/CivicPulse_Public.rds")

# Create party indicators
table(g$PartySummary)
g$Democrat <- ifelse(g$PartySummary == "Democrat", 1, 0)
g$Republican <- ifelse(g$PartySummary == "Republican", 1, 0)

# Create function to create summary statistics
file <- "tables/tbl_summarystat_civicpulse.txt"
datasummary(
  formula = College + Woman + (Minority = Minority_bin) + Democrat + Republican + (Conservative = con) + (Liberal = lib) +
    (`Non-Metro County` = Nonmetro_county) + (`County Government` = gov_county) + (`Municipal Government` = gov_muni) +
    (`Township Government` = gov_town) + Sample ~ Mean + SD + N + Percent(),
  data = g,
  fmt = 2,
  escape = FALSE,
  output = "latex"
) %>%
  cat(., file = file)
# Trim table shell
tbl <- readLines(file, warn = FALSE)
tbl <- tbl[-c(1,2,22)]
cat(tbl, file = file)
